import Vue from 'vue'
import { generatorDynamicRouter } from '@/core/start/generateRoutes'
import auth from '@/core/directives/auth'

const user = {
  state: {
    userInfo: {},
    permissions: [],
    permissionIds: [],
    addRouters: []
  },

  mutations: {
    SET_INFO: (state, info) => {
      Vue.ls.set('userInfo', info)
      state.userInfo = info
    },
    SET_ROUTERS: (state, routers) => {
      console.log(routers)
      Vue.ls.set('addRouters', routers)
      state.addRouters = routers
    },
    SET_PERMISSIONS: (state, list) => {
      state.permissions = list
      const permissionIds = list.map(p => {
        return p.name
      })
      state.permissionIds = permissionIds
      auth(permissionIds, Vue)
    }
  },
  actions: {
    GenerateRoutes ({ commit }, data) {
      return new Promise(resolve => {
        const routers = generatorDynamicRouter(data)
        commit('SET_ROUTERS', routers)
        resolve(routers)
      })
    }
  }
}

export default user
